//
//  ParitiesController.m
//  OnePiece
//
//  Created by lanou on 15/11/2.
//  Copyright © 2015年 wsy. All rights reserved.
//

#import "ParitiesController.h"
#import "DropDown.h"
#import "ParitiesModel.h"
#import "OPRequesterManager.h"
@interface ParitiesController () <CaculateDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UITextField *countTF;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIView *seperateView;
@property (nonatomic, strong) UILabel *moneyLabel;
@property (nonatomic, strong) DropDown *dropDown;
@property (nonatomic, strong) ParitiesModel *model;

@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, strong) NSMutableArray *tableNameArray;

@property (nonatomic, strong) UILabel *noticeLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@end


@implementation ParitiesController

-(NSMutableArray *)dataSourceArray
{
    if (_dataSourceArray == nil) {
        _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}

- (NSMutableArray *)tableNameArray
{
    if (_tableNameArray == nil) {
        _tableNameArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _tableNameArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor colorWithRed:0.859 green:0.463 blue:0.000 alpha:1.000];
    self.topView.backgroundColor = [UIColor colorWithRed:0.859 green:0.463 blue:0.000 alpha:1.000];
    self.titleLable.text = @"汇率";
    self.titleLable.textColor = [UIColor whiteColor];
    [self.leftButton addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    [self.leftButton setImage:[UIImage imageNamed:@"iconfont-weibiaoti1"] forState:(UIControlStateNormal)];
    
    
    _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-125, 100, 60, 30)];
    _nameLabel.text = @"人民币";
    _nameLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:_nameLabel];
    
    _countTF = [[UITextField alloc]initWithFrame:CGRectMake(50, 150, SCREEN_WIDTH-75, 30)];
    _countTF.keyboardType = UIKeyboardTypeNumberPad;
    _countTF.textAlignment = NSTextAlignmentRight;
    _countTF.textColor = [UIColor whiteColor];
    _countTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _countTF.placeholder = @"请输入金额";
    _countTF.delegate = self;
    
    [self.view addSubview:_countTF];
    
    UIView *singleView = [[UIView alloc]initWithFrame:CGRectMake(50, 180, SCREEN_WIDTH-75, 1)];
    singleView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:singleView];
    

    
    _moneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 250, SCREEN_WIDTH-75, 30)];
    _moneyLabel.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:_moneyLabel];

    UIView *singleView1 = [[UIView alloc]initWithFrame:CGRectMake(50, 281, SCREEN_WIDTH-75, 1)];
    singleView1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:singleView1];
    
    _dropDown = [[DropDown alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-125, 210, 110, 30)];
    _dropDown.tableArray = self.tableNameArray;
    _dropDown.delegate = self;
    _dropDown.kindLabel.text = @"美元";
    _dropDown.kindLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:_dropDown];
    
    [self configureOthers];
    [self requestData];
    
    
    
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)configureOthers
{
    _noticeLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 310, SCREEN_WIDTH, 20)];
    _noticeLabel.text = @"更新时间";
    _noticeLabel.textColor = [UIColor whiteColor];
    
    _dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 330, SCREEN_WIDTH, 20)];
    _dateLabel.font = [UIFont systemFontOfSize:12];
    _dateLabel.textColor = [UIColor whiteColor];
    
    [self.view addSubview:_noticeLabel];
    [self.view addSubview:_dateLabel];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_countTF resignFirstResponder];
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    double a = [[_dataSourceArray[_dropDown.i] fSellPri] doubleValue];
    double b = 100/a;
    double before =  [_countTF.text doubleValue] * b;
    _moneyLabel.text = [NSString stringWithFormat:@"%.2f",before];
    _moneyLabel.textColor = [UIColor whiteColor];
    return YES;
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_countTF resignFirstResponder];
    CGRect tv = _dropDown.tv.frame;
    tv.size.height = 0;
    _dropDown.tv.frame = tv;
    
    [self didEndSelectedRow];
}

- (void)didEndSelectedRow
{
    NSLog( @"%@",[_dataSourceArray[_dropDown.i] fSellPri]);
    double a = [[_dataSourceArray[_dropDown.i] fSellPri] doubleValue];
    double b = 100/a;
    double before =  [_countTF.text doubleValue] * b;
    _moneyLabel.text = [NSString stringWithFormat:@"%.2f",before];
    _moneyLabel.textColor = [UIColor whiteColor];
    _dateLabel.text = [NSString stringWithFormat:@"%@  %@", [_dataSourceArray[_dropDown.i] date], [_dataSourceArray[_dropDown.i] time]];
}


- (void)requestData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    
    [OPRequesterManager requestWithType:GET urlString:@"http://web.juhe.cn:8080/finance/exchange/rmbquot?key=6137c250cdd4b83c7fabe5512f478827" parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        
        for (int i = 1; i < ((NSDictionary *)dic[@"result"][0]).count + 1; i ++) {
            NSDictionary *dict = [dic[@"result"][0] valueForKey:[NSString stringWithFormat:@"data%d",i]];
            _model = [[ParitiesModel alloc]init];
            [_model setValuesForKeysWithDictionary:dict];
            [self.dataSourceArray addObject:_model];
            
            [self.tableNameArray addObject:dict[@"name"]];
        }
        [_dropDown.tv reloadData];

    } error:^(NSError *error) {
        NSLog(@"请求数据");
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
